//Loads the correct sidebar on window load,
//collapses the sidebar on window resize.
// Sets the min-height of #page-wrapper to window size
$(function() {
    $(window).bind("load resize", function() {
        topOffset = 50;
        width = (this.window.innerWidth > 0) ? this.window.innerWidth : this.screen.width;
        if (width < 768) {
            $('div.navbar-collapse').addClass('collapse')
            topOffset = 100; // 2-row-menu
        } else {
            $('div.navbar-collapse').removeClass('collapse')
        }

        height = (this.window.innerHeight > 0) ? this.window.innerHeight : this.screen.height;
        height = height - topOffset;
        if (height < 1) height = 1;
        if (height > topOffset) {
            $("#page-wrapper").css("min-height", (height) + "px");
        }
    });
    
    $('#goTop').stop().animate({
            bottom: '-100px'    
    },0);
    
    $('#goTop').click(function() {
        $('html, body').stop().animate({
                scrollTop: 0
        }, 500, function() {
                $('#goTop').stop().animate({
                        bottom: '-100px'    
                }, 500);
        });
    });
    
    $(window).scroll(function() {
        if($(this).scrollTop() > 100){
            $('#goTop').stop().animate({
                    bottom: '20px'    
            }, 500);
        }
        else{
            $('#goTop').stop().animate({
                    bottom: '-100px'    
            }, 500);
        }
    });
    
    $(".numeric-only").on("keyup", function(){
        this.value = this.value.replace(/[^\d.-]/g, '');
    });
});

Number.prototype.formatMoney = function(c, d, t){
var n = this, 
    c = isNaN(c = Math.abs(c)) ? 2 : c, 
    d = d == undefined ? "." : d, 
    t = t == undefined ? "," : t, 
    s = n < 0 ? "-" : "", 
    i = parseInt(n = Math.abs(+n || 0).toFixed(c)) + "", 
    j = (j = i.length) > 3 ? j % 3 : 0;
   return s + (j ? i.substr(0, j) + t : "") + i.substr(j).replace(/(\d{3})(?=\d)/g, "$1" + t) + (c ? d + Math.abs(n - i).toFixed(c).slice(2) : "");
 };
 
 $.fn.goToElement = function() {
    var elm = "html, body";
    if($('.modal').hasClass('in')){
        elm = ".modal";
    }
    $(elm).animate({
        scrollTop: $(this).offset().top + 'px'
    }, 'fast');
    return this; // for chaining...
}

var re_weburl = new RegExp(
  "^" +
    // protocol identifier
    "(?:(?:https?|ftp)://)" +
    // user:pass authentication
    "(?:\\S+(?::\\S*)?@)?" +
    "(?:" +
      // IP address exclusion
      // private & local networks
      "(?!(?:10|127)(?:\\.\\d{1,3}){3})" +
      "(?!(?:169\\.254|192\\.168)(?:\\.\\d{1,3}){2})" +
      "(?!172\\.(?:1[6-9]|2\\d|3[0-1])(?:\\.\\d{1,3}){2})" +
      // IP address dotted notation octets
      // excludes loopback network 0.0.0.0
      // excludes reserved space >= 224.0.0.0
      // excludes network & broacast addresses
      // (first & last IP address of each class)
      "(?:[1-9]\\d?|1\\d\\d|2[01]\\d|22[0-3])" +
      "(?:\\.(?:1?\\d{1,2}|2[0-4]\\d|25[0-5])){2}" +
      "(?:\\.(?:[1-9]\\d?|1\\d\\d|2[0-4]\\d|25[0-4]))" +
    "|" +
      // host name
      "(?:(?:[a-z\\u00a1-\\uffff0-9]-*)*[a-z\\u00a1-\\uffff0-9]+)" +
      // domain name
      "(?:\\.(?:[a-z\\u00a1-\\uffff0-9]-*)*[a-z\\u00a1-\\uffff0-9]+)*" +
      // TLD identifier
      "(?:\\.(?:[a-z\\u00a1-\\uffff]{2,}))" +
    ")" +
    // port number
    "(?::\\d{2,5})?" +
    // resource path
    "(?:/\\S*)?" +
  "$", "i"
);

function Message(message){
    this.error = true;
    this.result = "";
    
    var _res = new Object();
    try{
        _res = JSON.parse(message);
    } catch(e) {}
    
    if(typeof _res.error != 'undefined' && typeof _res.result != 'undefined'){
        for(i in _res){
            this[i] = _res[i];
        }
        
        //force reload
        if(typeof this.need_reload != 'undefined'){
            if(this.need_reload){
                location.reload();
            }
        }
    } else {
        this.result = message.replace(/(<([^>]+)>)/ig,"");
    }
}

function AjaxPost(url){
    var url = url;
    
    this.contentType = 'application/x-www-form-urlencoded; charset=UTF-8';
    this.crossDomain = false;
    this.data = {};    
    this.async = true;
    this.processData = true;    
    this.timeout = 60000;
    
    this.error = function(xhr){
        alert("Tidak dapat tersambung ke server");
    };
    this.success = function(response){
        alert("Sukses");
    };
  
    this.doPost = function(obj){
        var is_object = false;
        
        try {
            is_object = obj instanceof HTMLElement;
        }
        catch(e){
            is_object = (typeof obj==="object") &&
              (obj.nodeType===1) && (typeof obj.style === "object") &&
              (typeof obj.ownerDocument ==="object");
        }
        
        if(typeof obj != "undefined"){
            if(is_object){
                $(obj).button('loading');
            } else {
                if(obj.charAt(0) != "." && obj.charAt(0) != "#"){
                    $("#" + obj).button('loading');
                } else {
                    $(obj).button('loading');
                }
            }
        }
        
        $.ajax({
            processData: this.processData, 
            contentType: this.contentType,
            crossDomain: this.crossDomain,
            async: this.async,
            url: url,
            data: this.data,
            type:'POST',
            timeout: this.timeout,
            success: this.success,
            error: this.error,
            statusCode: {
                404: function() {
                    alert("Halaman Tidak Ditemukan");
                }
            },
            complete: function(){
                if(typeof obj != "undefined"){
                    if(is_object){
                        $(obj).button('reset');
                    } else {
                        if(obj.charAt(0) != "." && obj.charAt(0) != "#"){
                            $("#" + obj).button('reset');
                        } else {
                            $(obj).button('reset');
                        }
                    }
                }
            }
        });
    };
}